package readCSV;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Vector;


import dao.*;

public class ReadCSV {

/**
 * @param args
 */
	public static void main(String[] args)  {
		
		
		CreateTables.Create("./bd");

		try{
			ConnectionManager.connect("./bd");
		}
		catch(SQLException ex){
			System.out.println("Error while connecting to db");
		}
		catch(ClassNotFoundException ex){
			System.out.println("Class not found, check classpath");
		}
	
		
				
		
		String [] fields;
		String line;
		try {
		  
			
			FileReader reader = new FileReader("src/readCSV/birds_info.csv");
			BufferedReader br = new BufferedReader(reader);
					
			br.readLine();
			
			while ( (line = br.readLine()) != null )
			{
				fields = line.split("\\*");
				
							
				String ScientificName = fields[0];
				String [] CommonNames = fields[1].substring(1, fields[1].length()-1).split("\\,");
				String RoE = fields[2];
				float Size = Float.parseFloat(fields[3]);
				float Weight = Float.parseFloat(fields[4]);
				String Color = fields[5];
				float Longevity = Float.parseFloat(fields[6]); 
				float ClutchSize = Float.parseFloat(fields[7]);
				float IncubationTime = Float.parseFloat(fields[8]);
				String Diet = fields[9]; 
				String Habitat = fields[10];
				String [] Countries = fields[11].substring(1, fields[11].length()-1).split("\\,");
				String ImageURL = fields[12];
				 
								
				try{
				
				BirdVO bird = new BirdVO(ScientificName,RoE,ImageURL,Weight,Habitat,IncubationTime,Longevity,Color,Size,ClutchSize,Diet);
				BirdDAO.writeBird(bird);
				
				
							  
				
				for (int i=0 ; i<Countries.length; i++)
				 {					 
					// CountryVO country = new CountryVO(ScientificName, Countries[i]); 
					 
						BirdDAO.addCountry(Countries[i], bird);
						 //CountryDAO.writeCountry(country);
					
				 }
				  
				 for (int i=0 ; i<CommonNames.length; i++)
				 {
					// PopularNamesVO pname = new PopularNamesVO(CommonNames[i], ScientificName); 
					
						BirdDAO.addPopularName(CommonNames[i], bird);
						 //PopularNamesDAO.writePopularName(pname);
					
				}
				
				}catch (SQLException e){
					e.printStackTrace();
				}
			  
			  
			}
		  
	    
	  
	  } catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	  catch (IOException e)
	  {
		  e.printStackTrace();
	  }
	
	
		//TESTE 1
		/*
		try {
			System.out.println("TESSST");
		
		
			Vector<BirdVO> bb = BirdDAO.arbitrarySQL("select * from bird");
		for(int i=0;i<bb.size();i++)
		{
			System.out.println(bb.get(i).getScientific_name());
			BirdVO b = BirdDAO.findByScientificName(bb.get(i).getScientific_name());
			System.out.println(b.getImageurl());
			Vector<CountryVO> c = CountryDAO.selectByBird_scientific_name(bb.get(i).getScientific_name());
				for(int j=0;j<c.size();j++)
					System.out.println("\t\t"+c.get(j).getCountry());
			
		}		
		} catch (SQLException e){
			e.printStackTrace();
		}
		*/

}

}
